package com.dhnsoft.server;

import com.dhnsoft.game.GameFrame;
import com.dhnsoft.tank.MyTank;
import com.dhnsoft.tank.Tank;

import java.io.*;
import java.net.Socket;

/**
 * @Description
 * @ClassName NetClient
 * @Author dhn
 * @date 2020.11.25 11:06
 */
public class NetClient {
    //持有坦克引用
    GameFrame tc;
    //udp端口号
    private static int UDP_PORT_STATE = 2223;
    public NetClient(GameFrame tc){
        udpPort = UDP_PORT_STATE++;
        this.tc = tc;
    }
    //定义UDP端口
    private int udpPort;
    public void connect(String IP,int port){
        Socket s=null;
        try {
            s = new Socket(IP,port);
            //DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            //向服务器端发送数据
            //dos.writeInt(222);
            DataInputStream dis = new DataInputStream(s.getInputStream());
            int id = dis.readInt();
            System.out.println("id:"+id);
            tc.getMyTank().setId(id);
                String s1 = dis.readUTF();
                System.out.println(s1+"...");

            System.out.println("Connect Success and server give me a ID:"+id);
            System.out.println("Connect Success and server give me a ID:");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(s!=null){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
//        File src = new File("D:/a.txt");
//        //FileOutputStream fos = null;
//        PrintWriter printWriter  =  null;
//        OutputStreamWriter writer = null;
//        try {
//            //fos = new FileOutputStream(src);
//            printWriter = new PrintWriter(src);
//
//            // 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
//            printWriter.write("123");
//            printWriter.flush();
//            //fos.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        File src = new File("D:/a.txt");
        String sResult="";
        try {
            InputStreamReader reader=new InputStreamReader(new FileInputStream(src),"gbk");
            BufferedReader br=new BufferedReader(reader);
            String s=null;
//            while((s=br.readLine())!=null) {
//                sResult+=s;
//                System.out.println(s);
//            }
            String s1 = br.readLine();
            System.out.println(s1);
        }catch (Exception e) {
            e.printStackTrace();
        }


    }
}
